package com.example.xianji.activity;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.List;

import com.example.xianji.R;
import com.example.xianji.adapter.Search_Adapter;
import com.example.xianji.constant.Constant;
import com.example.xianji.fragment.ChooseDaTingFragment;
import com.example.xianji.fragment.FuWu_Fragment;
import com.example.xianji.fragment.XuQiuFragment;
import com.example.xianji.utils.sp.PreferenceEngine;

import android.os.Bundle;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 搜索页面
 *
 * @author me
 */
public class Main_SearchActivity extends Activity implements OnClickListener {
    public static List<String> list = new ArrayList<String>();
    public static List<String> list1 = new ArrayList<String>();
    //    private Editor edit;
//    private SharedPreferences mySharedPreferences;
    public static String text_zhaoping_content = "";
    /**
     * 点击搜索的状态
     */
    public static int fuwu_search_id, xuqiu_search_id, zhaoping_search_id, skill_search_id, junming_search_id, qiye_search_id, qiye_product_id = 0, zhanhui_id = 0, news_search_id = 0;
    private ImageView search_back, seach_image_delete;
    private Button sousuo;
    private EditText editText_search;
    private RelativeLayout relativelayout_jilu;
    private ListView listview_jilu;
    private Search_Adapter mSearch_Adapter;
    private TextView delect_jilu;

    /**
     * 将list集合转换成字符串
     *
     * @param SceneList
     * @return
     * @throws IOException
     */
    public static String SceneList2String(List<String> SceneList) throws IOException {
        // 实例化一个ByteArrayOutputStream对象，用来装载压缩后的字节文件。
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        // 然后将得到的字符数据装载到ObjectOutputStream
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                byteArrayOutputStream);
        // writeObject 方法负责写入特定类的对象的状态，以便相应的 readObject 方法可以还原它
        objectOutputStream.writeObject(SceneList);
        // 最后，用Base64.encode将字节文件转换成Base64编码保存在String中
        String SceneListString = new String(Base64.encode(
                byteArrayOutputStream.toByteArray(), Base64.DEFAULT));
        // 关闭objectOutputStream
        objectOutputStream.close();
        return SceneListString;
    }

    /**
     * 将字符串转换成list集合
     *
     * @param SceneListString
     * @return
     * @throws StreamCorruptedException
     * @throws IOException
     * @throws ClassNotFoundException
     */

    @SuppressWarnings("unchecked")
    public static List<String> String2SceneList(String SceneListString)
            throws StreamCorruptedException, IOException,
            ClassNotFoundException {
        byte[] mobileBytes = Base64.decode(SceneListString.getBytes(),
                Base64.DEFAULT);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                mobileBytes);
        ObjectInputStream objectInputStream = new ObjectInputStream(
                byteArrayInputStream);
        List<String> SceneList = (List<String>) objectInputStream.readObject();
        objectInputStream.close();
        return SceneList;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_main__search);


        //	XJApplication.getInstance3().addActivity3(this);


        search_back = (ImageView) findViewById(R.id.search_back);
        sousuo = (Button) findViewById(R.id.sousuo);

        editText_search = (EditText) findViewById(R.id.editText_search);
        relativelayout_jilu = (RelativeLayout) findViewById(R.id.relativelayout_jilu);
        listview_jilu = (ListView) findViewById(R.id.listview_jilu);
        delect_jilu = (TextView) findViewById(R.id.delect_jilu);
        seach_image_delete = (ImageView) findViewById(R.id.seach_image_delete);
//        mySharedPreferences = getSharedPreferences("text", MODE_PRIVATE);
//        edit = mySharedPreferences.edit();
        String liststr = PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_ARRAY_LIST, "");
        try {
            list1 = String2SceneList(liststr);
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        mSearch_Adapter = new Search_Adapter(this, list1);
        if (list1.size() > 0) {
            relativelayout_jilu.setVisibility(View.VISIBLE);
            listview_jilu.setVisibility(View.VISIBLE);

            listview_jilu.setAdapter(mSearch_Adapter);

        } else {
            relativelayout_jilu.setVisibility(View.GONE);
            listview_jilu.setVisibility(View.GONE);


        }

        if (getIntent().getIntExtra("search", 0) == 1) {
            editText_search.setText(XuQiuFragment.text_xuqiu_content);

        } else if (getIntent().getIntExtra("search", 0) == 2) {

            editText_search.setText(FuWu_Fragment.text_fuwu_content);
        } else if (getIntent().getIntExtra("search", 0) == 3) {
            editText_search.setText(text_zhaoping_content);

        } else if (getIntent().getIntExtra("search", 0) == 4) {
            editText_search.setText(Main_Skill_Activity.text_skill_content);

        } else if (getIntent().getIntExtra("search", 0) == 5) {
            editText_search.setText(Main_JunMing_RongHeActivity.text_junming_content);

        } else if (getIntent().getIntExtra("search", 0) == 6) {
            editText_search.setText(Main_Find_QiYeActivity.text_qiye_content);

        } else if (getIntent().getIntExtra("search", 0) == 7) {
            editText_search.setText(ChooseDaTingFragment.text_product_context);


        } else if (getIntent().getIntExtra("search", 0) == 8) {
            editText_search.setText(Main_ZhanHui_DaTingActivity.text_zhanhui_context);
        } else if (getIntent().getIntExtra("search", 0) == 9) {
            editText_search.setText(Main_newsActivity.newsSearchContant);
        }

        if (editText_search.getText().toString().length() == 0) {
            seach_image_delete.setVisibility(View.GONE);
        } else {
            seach_image_delete.setVisibility(View.VISIBLE);
        }

        editText_search.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() == 0) {
                    seach_image_delete.setVisibility(View.GONE);
                } else {
                    seach_image_delete.setVisibility(View.VISIBLE);
                }
            }
        });

        listview_jilu.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                if (getIntent().getIntExtra("search", 0) == 1) {
                    XuQiuFragment.text_xuqiu_content = list1.get(arg2);
                    xuqiu_search_id = 1;
                } else if (getIntent().getIntExtra("search", 0) == 2) {
                    fuwu_search_id = 1;
                    FuWu_Fragment.text_fuwu_content = list1.get(arg2);
                } else if (getIntent().getIntExtra("search", 0) == 3) {
                    text_zhaoping_content = list1.get(arg2);
                    zhaoping_search_id = 1;
                } else if (getIntent().getIntExtra("search", 0) == 4) {
                    Main_Skill_Activity.text_skill_content = list1.get(arg2);
                    skill_search_id = 1;
                } else if (getIntent().getIntExtra("search", 0) == 5) {
                    Main_JunMing_RongHeActivity.text_junming_content = list1.get(arg2);
                    junming_search_id = 1;
                } else if (getIntent().getIntExtra("search", 0) == 6) {
                    Main_Find_QiYeActivity.text_qiye_content = list1.get(arg2);
                    qiye_search_id = 1;
                } else if (getIntent().getIntExtra("search", 0) == 7) {
                    ChooseDaTingFragment.text_product_context = list1.get(arg2);
                    qiye_product_id = 1;
                } else if (getIntent().getIntExtra("search", 0) == 8) {
                    Main_ZhanHui_DaTingActivity.text_zhanhui_context = list1.get(arg2);
                    zhanhui_id = 1;
                } else if (getIntent().getIntExtra("search", 0) == 9) {
                    Main_newsActivity.newsSearchContant = list1.get(arg2);
                    news_search_id = 1;
                }
                Main_SearchActivity.this.finish();
            }
        });
        search_back.setOnClickListener(this);
        sousuo.setOnClickListener(this);
        delect_jilu.setOnClickListener(this);
        seach_image_delete.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.search_back) {
            Main_SearchActivity.this.finish();
        } else if (v.getId() == R.id.sousuo) {
            if (editText_search.getText().toString().length() > 0) {
                if (list1.contains(editText_search.getText().toString())) {
                } else {
                    list1.add(editText_search.getText().toString());
                }
                try {
                    String liststr = SceneList2String(list1);
                    PreferenceEngine.getInstance().putString(Constant.SP.NAME, Constant.SP.KEY_ARRAY_LIST, liststr);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (getIntent().getIntExtra("search", 0) == 1) {
                XuQiuFragment.text_xuqiu_content = editText_search.getText().toString();
                xuqiu_search_id = 1;
            } else if (getIntent().getIntExtra("search", 0) == 2) {
                fuwu_search_id = 1;
                FuWu_Fragment.text_fuwu_content = editText_search.getText().toString();
            } else if (getIntent().getIntExtra("search", 0) == 3) {
                text_zhaoping_content = editText_search.getText().toString();
                zhaoping_search_id = 1;
            } else if (getIntent().getIntExtra("search", 0) == 4) {
                Main_Skill_Activity.text_skill_content = editText_search.getText().toString();
                skill_search_id = 1;
            } else if (getIntent().getIntExtra("search", 0) == 5) {
                Main_JunMing_RongHeActivity.text_junming_content = editText_search.getText().toString();
                junming_search_id = 1;
            } else if (getIntent().getIntExtra("search", 0) == 6) {
                Main_Find_QiYeActivity.text_qiye_content = editText_search.getText().toString();
                qiye_search_id = 1;
            } else if (getIntent().getIntExtra("search", 0) == 7) {
                ChooseDaTingFragment.text_product_context = editText_search.getText().toString();
                qiye_product_id = 1;
            } else if (getIntent().getIntExtra("search", 0) == 8) {
                Main_ZhanHui_DaTingActivity.text_zhanhui_context = editText_search.getText().toString();
                zhanhui_id = 1;
            } else if (getIntent().getIntExtra("search", 0) == 9) {
                Main_newsActivity.newsSearchContant = editText_search.getText().toString();
                news_search_id = 1;
            }
            Main_SearchActivity.this.finish();
        } else if (v.getId() == R.id.delect_jilu) {
            list1.clear();
            try {
                String liststr = SceneList2String(list1);
                PreferenceEngine.getInstance().putString(Constant.SP.NAME, Constant.SP.KEY_ARRAY_LIST, liststr);
            } catch (IOException e) {
                e.printStackTrace();
            }
            listview_jilu.setAdapter(mSearch_Adapter);
            relativelayout_jilu.setVisibility(View.GONE);
            listview_jilu.setVisibility(View.GONE);
        } else if (v.getId() == R.id.seach_image_delete) {
            editText_search.setText("");
        }
    }
}
